#include "StdAfx.h"
#include "Step.h"
#include "Action.h"
#include "Case.h"
#include "../macro.h"
Step::Step(Case* parent)
	: parent_(parent)
{
}

Step::~Step(void)
{
}

bool Step::addAction(Action* action)
{
	if (action==NULL)
	{
		return false;
	}

	m_actions.push_back(action);
	return true;
}

bool Step::delAction(int index)
{
	if (index>=m_actions.size())
	{
		return false;
	}

	int id_of_list = 0;
	for (std::vector<Action*>::iterator iter=m_actions.begin();iter!=m_actions.end();++iter,++id_of_list)
	{
		if (index == id_of_list)
		{
			m_actions.erase(iter);
			break;
		}
	}
	return true;
}

bool Step::run(void* param)
{
	DBG_PRINTF("%s\r\n",__FUNCTION__);

	MSG_PRINTF("    [step]\r\n");
	for (float index = getStart(); index < getEnd(); index += getStep())
	{
		for (std::vector<Action*>::iterator iter = m_actions.begin(); iter != m_actions.end(); ++iter)
		{
			if ((*iter) != NULL)
			{
				if (!(*iter)->run(param))
				{
					return false;
				}
			}
		}
	}

	return true;
}

std::list<loopable *> Step::getLoopable()
{
	std::list<loopable *>  loop;
	if (parent_)
	{
		loop = parent_->getLoopable();
	}

	loop.push_back(this);

	return loop;
}